package com.examination.to.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.examination.to.entity.Exam;
import com.examination.to.entity.Question;
import com.examination.to.service.impl.ExamServiceImpl;
import com.examination.to.service.impl.QuestionServiceImpl;
import com.examination.to.util.Result;
import com.examination.to.vo.QuestionVo;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Slf4j
@RequestMapping("/exam")
@Api(tags = "考试及试题创建修改等")
public class CreateExamController {

    @Autowired
    private ExamServiceImpl examService;

    @Autowired
    private QuestionServiceImpl questionServiceImpl;

    @PostMapping("/create")
    @ApiOperation("创建考试")
    public Result createExam(@RequestBody Exam exam){
        Result result = examService.createExam(exam);
        log.info(exam.toString());
        return result;
    }

    @PostMapping("/delete")
    @ApiOperation("删除该考试")
    public Result deleteExam(@RequestBody Exam exam){
        exam.setExamname(exam.getExamname());
        Result result = examService.deleteExam(exam);
        log.info(result.toString());
        return result;
    }

    @PostMapping("/update")
    @ApiOperation("更改考试属性字段值")
    public Result updateExam(@RequestBody Exam exam){
        return examService.updateExam(exam);
    }

    @PostMapping("/addQustion")
    @ApiOperation("添加试题")
    public Result addQustion(@RequestBody QuestionVo questionVo){
        log.info(questionVo.toString());
        Result result = questionServiceImpl.addQustion(questionVo);
        return result;
    }

    @PostMapping("/addQuestionByBatch")
    @ApiOperation("批量添加试题")
    public Result addQuestionByBatch(@RequestBody List<QuestionVo> questionVos){
        Result result = questionServiceImpl.addQuestionByBatch(questionVos);
        return result;
    }

    @PostMapping("/deleteQuestion")
    @ApiOperation("删除试题")
    public Result deleteQuestion(@RequestBody Question question){
        Result result = questionServiceImpl.deleteQustion(question);
        return result;
    }

    @PostMapping("/updateQuestion")
    @ApiOperation("批量修改试题")
    public Result updateQuestionByBatch(@RequestBody List<QuestionVo> questionVos){
        Result result = questionServiceImpl.updateQuestionByBatch(questionVos);
        return result;
    }

    @PostMapping("/showQuestions")
    @ApiOperation("渲染问题列表")
    public List<QuestionVo> showQuestions(@RequestBody Exam exam){
        return questionServiceImpl.showQuestions(exam);
    }

    @GetMapping("/examList")
    @ApiOperation("返回exam列表")
    public List<Exam> examList(){
        return examService.examList();
    }

    @ApiOperation("分页渲染问题列表")
    @GetMapping("/getQuestions")
    public Result getQuestions(@RequestParam(required = false)Integer examid,
                               Integer pageNo,Integer pageSize){
        IPage<Question> questionIPage = new Page<>(pageNo,pageSize);
        QueryWrapper<Question> queryWrapper = new QueryWrapper();
        if (examid != null){
            queryWrapper.eq("examid",examid);
        }
        IPage<Question> page = questionServiceImpl.page(questionIPage,queryWrapper);
        List<Question> questions = page.getRecords();
        Map<Object,Object> result1 = new HashMap<>();
        result1.put("questions",questions);
        result1.put("total",questionIPage.getTotal());
        Result result = new Result();
        result.setCode(200);
        result.setMsg("获取分页数据成功");
        result.setDetail(result1);
        return result;
    }

    @GetMapping("/pageExams")
    @ApiOperation("实现试卷布局的分布功能")
    public Result pageExams(Integer pageNo,Integer pageSize){
        IPage<Exam> examIPage = new Page<>(pageNo,pageSize);
        QueryWrapper<Exam> queryWrapper = new QueryWrapper<>();
        List<Exam> examList = examService.list(queryWrapper);
        Result result = new Result();
        if (examList == null){
            result.setMsg("没有试卷，试卷列表为空");
            result.setCode(-1);
            result.setDetail(null);
        }else{
            IPage<Exam> page = examService.page(examIPage,queryWrapper);
            List<Exam> exams = page.getRecords();
            long pageMounts = page.getPages();
            Map<Object,Object> result1 = new HashMap<>();
            result1.put("pages",pageMounts);
            result1.put("total",examIPage.getTotal());
            result1.put("exams",exams);
            result.setDetail(result1);
            result.setCode(200);
            result.setMsg("获取试卷分页数据成功");
        }
        return result;
    }

    @PostMapping("/issueAndshow")
    @ApiOperation("发布并分享")
    public Result issueAndshow(@RequestParam(required = false)Integer id,
                               @RequestBody List<QuestionVo> questionVos){
        return examService.issueAndshow(id,questionVos);
    }

//    @PostMapping("/getExamUrl")
//    public String getExamUrl(@RequestBody Exam exam){
//        return examService.getExamUrl(exam);
//    }
}
